package com.yang.mapper;

import com.yang.entity.OrderDetail;
import com.yang.entity.Orders;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface OrderMapper {

    /**
     * 插入订单信息
     *
     * @param order 订单信息
     */
    @Insert("insert into orders value(default, #{userId}, #{totalAmount}, #{paymentMethod}, #{createTime}, null, null)")
    void insertOrder(Orders order);

    /**
     * 获取最新插入的订单id
     */
    @Select("select orders_id from orders where user_id = #{userId} and create_time = (select max(create_time) from orders)")
    long getInsertId(Long userId);

    void insertOrderDetailList(List<OrderDetail> orderDetailList);

    /**
     * 获取订单详情状态
     *
     * @param userId 用户id
     * @return 订单状态
     */
    @Select("select count(*) from order_detail where user_id = #{userId} and status = 1")
    int getOrderStatus(Long userId);

    /**
     * 更新订单状态
     *
     * @param i       状态id
     * @param orderId 订单id
     * @param times   时间戳
     */
    @Update("update order_detail set status = #{i}, update_time = #{times} where orders_id = #{orderId}")
    void updateOrderStatus(long orderId, int i, String times);

    /**
     * 根据订单详情id更新地址
     *
     * @param addressId     地址id
     * @param orderDetailId 订单详情id
     * @param updateTime    更新时间
     * @return 更新的行数
     */
    int updateAddressOfOrders(List<Integer> orderDetailId, Integer addressId, String updateTime);

    /**
     * 获取订单列表，用xml映射
     *
     * @param userId 用户id
     * @return 订单列表
     */
    List<OrderDetail> getOrders(int userId);

    @Select("select user_id from orders where orders_id = #{orderId}")
    long getUserIdByOrderId(long orderId);
}
